#!/bin/sh

cur_path=`pwd`
POD_PROJECT="${cur_path}/Example/Pods/Pods.xcodeproj"
BUILD_DIR="${cur_path}/Build_Dir"
TARGET_NAME='ACFramework'
FRAMEWORK_NAME=${TARGET_NAME}.framework
DEBUG_IPHONEOS=${BUILD_DIR}/Debug-iphoneos/${TARGET_NAME}/${FRAMEWORK_NAME}
DEBUG_SIMULATOR=${BUILD_DIR}/Debug-iphonesimulator/${TARGET_NAME}/${FRAMEWORK_NAME}
RELEASE_IPHONEOS=${BUILD_DIR}/Release-iphoneos/${TARGET_NAME}/${FRAMEWORK_NAME}
RELEASE_SIMULATOR=${BUILD_DIR}/Release-iphonesimulator/${TARGET_NAME}/${FRAMEWORK_NAME}

DEBUG_FRAMEWORK=${cur_path}/Frameworks/Debug/${FRAMEWORK_NAME}
RELEASE_FRAMEWORK=${cur_path}/Frameworks/Release/${FRAMEWORK_NAME}

# 编译 Framwork
# $1: Debug 或 Release
# $2: iphonesimulator 或 iphoneos
# $3: 架构
function buildAction() {
    xcodebuild -target ${TARGET_NAME} -project ${POD_PROJECT} ONLY_ACTIVE_ARCH=NO -configuration $1 -sdk $2 -arch $3 BUILD_DIR=${BUILD_DIR} clean build
}

 # 合并真机与模拟器的 Framwork
 # $1: 真机 Framework
 # $2: 模拟器 Framework
 # $3: 合并 Framework
function mergeFramework() {
    if [ -d $3 ]; then
        rm -rf $3
    fi
    mkdir -p $3

    cp -r $1/ $3
    lipo -create $1/${TARGET_NAME} $2/${TARGET_NAME} -output $3/${TARGET_NAME}
}

##############

if [ -d $BUILD_DIR ]; then
    rm -rf $BUILD_DIR
fi 

buildAction Debug iphonesimulator x86_64
buildAction Debug iphoneos arm64
buildAction Release iphonesimulator x86_64
buildAction Release iphoneos arm64

mergeFramework $DEBUG_IPHONEOS $DEBUG_SIMULATOR $DEBUG_FRAMEWORK
mergeFramework $RELEASE_IPHONEOS $RELEASE_SIMULATOR $RELEASE_FRAMEWORK

rm -rf $BUILD_DIR

